package MyScreen;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import model.AuditionCanvas;


public class MainPage extends MIDlet implements CommandListener {
	public Form  Help;
	Gioithieu gioithieu;

	Form fmhd;
	Form Choncapdo;
	ChoiceGroup choose;
	int songchoi=0;
	Form frabout;
	public List ls;
	int dolon=15;
	
	Display display;
	AuditionCanvas audition;
	public MainPage() {
		taolist();
		taoformabout();
		taoformhd();
		songchoi=0;
		Choncapdo = new Form("Choose Level");
		Choncapdo.addCommand(new Command("Back",Command.BACK,0));
		 choose=new ChoiceGroup("Level",Choice.EXCLUSIVE); 
		{ 
		 choose.append("Level 0", null); 
		 choose.append("Level 1", null); 
		 choose.append("Level 2", null); 
		 choose.append("Level 3", null); 
		 choose.append("Level 4", null); 
		 
		 };   

		Choncapdo.append(choose);
		Choncapdo.addCommand(new Command("OK",Command.OK,0)) ;
		Choncapdo.setCommandListener(this);
		
		
	    gioithieu=new Gioithieu(Display.getDisplay(this),ls);
	    ls.setCommandListener(this);
	    
    
    hienthi(ls);
	
	}
	public void newgame(int i)
	{
		if (audition==null)
		{
		audition=new AuditionCanvas(this,i);
		audition.start();
		hienthi(audition);
		return;
		}
		audition.reset(songchoi);
		hienthi(audition);
	}
	public void hienthilish()
	{
		hienthi(ls);
	}
	public void hienthi(Displayable d)
	{
		display=Display.getDisplay(this);
		display.setCurrent(d);
		
	}
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}
	public void taolist()
	{
		if (ls==null)
		{  try{
			String tc[]={"New Game","Choose Level",
				"Help","About","Exit"};
		    Image img[]={Image.createImage("/play.png"),
		    		Image.createImage("/seting.png"),
		    		Image.createImage("/help.png"),
		    		Image.createImage("/about.png"),
		    		Image.createImage("/exit.png")
		    		
		    		};
			ls=new List("Opition",List.IMPLICIT,tc,img);
			ls.addCommand(new Command("Exit",Command.BACK,0));
			ls.setCommandListener(this);
		}
		catch(Exception ex)
		{}
			
		}
		
	}
	public void taoformhd()
	{
		fmhd=new Form("Help");
		fmhd.append("On top of the arrows, it shows a dot moving to the right, press the button blue when the dot is on the white spot");
		fmhd.addCommand(new Command("Back",Command.BACK, 0));
		fmhd.setCommandListener(this);
		
	}
	
	
	public void taoformabout()
	{
		frabout=new Form("About");
		frabout.append("Application name: Audition\n" +
				"Developer name: Hoang Tan Duy\n" +
                       "The version number: 1.0.0\n");
		frabout.addCommand(new Command("Back",Command.BACK, 0));
		frabout.setCommandListener(this);
	}
	public void commandAction(Command c, Displayable d) {
		if (d==ls && c==List.SELECT_COMMAND) 
		{
			int i=ls.getSelectedIndex();
			switch (i) 
			{
			case 0: 
				
					newgame(songchoi);
					
				   
				
				break;
			case 1:
				hienthi(Choncapdo);
				break;
			case 2:
				hienthi(fmhd);
				break;
			case 3: 
				hienthi(frabout);
				break;
			case 4:
				notifyDestroyed();
				break;
			}
			
		}
		
		if (c.getLabel().equals("OK"))
		{
			
			if (choose.isSelected(0)==true)
			{
				songchoi=0;
			}
			if (choose.isSelected(1)==true)
			{
				songchoi=1;
			}
			if (choose.isSelected(2)==true)
			{
				songchoi=2;
			}
			if (choose.isSelected(3)==true)
			{
				songchoi=3;
			}
			if (choose.isSelected(4)==true)
			{
				songchoi=4;
			}
			
			hienthi(ls);
		}
		if (c.getLabel().equals("Back"))
		{	
			hienthi(ls);
		}
		if (c.getLabel().equals("Exit"))
		{	
			notifyDestroyed();
		}
	}

}
